//
//  AXFNewFeatureView.m
//  爱鲜蜂
//
//  Created by HH on 16/11/12.
//  Copyright © 2016年 胡浩. All rights reserved.
//

#import "AXFNewFeatureView.h"
#import "AXFTabBarController.h"

@interface AXFNewFeatureView ()<UIScrollViewDelegate>

@property (nonatomic,weak)UIPageControl *pageController;
@property (nonatomic,weak)UIButton *experienceBtn;

@end


@implementation AXFNewFeatureView

- (void)setupUI
{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];

    scrollView.delegate = self;
    
    scrollView.bounces = NO;
    
    scrollView.pagingEnabled = YES;
    
    scrollView.showsHorizontalScrollIndicator = NO;
    
    [self addSubview:scrollView];
    
    NSMutableArray<UIImageView *> *tempArr = [NSMutableArray array];
    
    for (int i = 0; i < 4; i++) {
        NSString *imageName = [NSString stringWithFormat:@"guide_40_%d",i + 1];
        
        UIImageView *imageView = [[UIImageView alloc] init];
        
        imageView.image = [UIImage imageNamed:imageName];
        
        [scrollView addSubview:imageView];
        
        [tempArr addObject:imageView];
    }
    
    NSArray *imageViewArr = tempArr.copy;
    
    UIPageControl *pageController = [[UIPageControl alloc] init];
    
    _pageController = pageController;
    
    pageController.numberOfPages = imageViewArr.count;
    
    pageController.pageIndicatorTintColor = [UIColor blackColor];
    
    pageController.currentPageIndicatorTintColor = [UIColor whiteColor];
    
    [self insertSubview:pageController aboveSubview:scrollView];
    
    UIButton *experienceBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    
    _experienceBtn = experienceBtn;
    
    [experienceBtn setTitle:@"立即体验" forState:UIControlStateNormal];
    
    [experienceBtn setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
    
    [experienceBtn setContentEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
    
    experienceBtn.layer.borderColor = [UIColor brownColor].CGColor;
    
    experienceBtn.layer.borderWidth = 1.0;
    
    experienceBtn.layer.cornerRadius = 5.0;
    
    experienceBtn.clipsToBounds = YES;
    
    experienceBtn.hidden = YES;
    
    [experienceBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:experienceBtn];
    
    //MARK:布局
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
    [imageViewArr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
    
    [imageViewArr mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo([UIScreen mainScreen].bounds.size);
        make.top.equalTo(scrollView);
    }];
    
    [pageController mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.bottom.offset(-30);
    }];
    
    [experienceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.bottom.equalTo(pageController.mas_top).offset(-10);
    }];
}


#pragma mark - ScrollView滚动时会调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint offset = scrollView.contentOffset;
    
    NSInteger index = offset.x / [UIScreen mainScreen].bounds.size.width;
    
    _pageController.currentPage = roundf(index);
    
    _experienceBtn.hidden = _pageController.currentPage == 3 ? NO : YES;
}

- (void)btnClick:(UIButton *)sender
{
    
    AXFTabBarController *tabBarController = [[AXFTabBarController alloc] init];
    
    self.superview.window.rootViewController = tabBarController;
//
//    if ([_delegate respondsToSelector:@selector(displayMainController)]) {
//        [_delegate displayMainController];
//    }
}

@end
